/**
 * 
 */
package com.hawkfly.uploader.ftp;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import netscape.javascript.JSObject;

/**
 * FTP实现的大文件上传
 * @author hawkfly
 */
public class FileFtpApplet extends Applet
{
    private static final long serialVersionUID=1L;

    private FileFtpApplet jFrame;

    private JButton jFileButton;
    private JTextArea textArea;
    private JProgressBar progressBar;

    public FileFtpApplet()
    {
        jFrame=this;
        this.setSize(196,260);
        textArea = new JTextArea("logArea", 20, 50);
        textArea.setVisible(true);
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        JScrollPane scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        progressBar = new JProgressBar(0,100);
        progressBar.setStringPainted(true);
        progressBar.setBackground(Color.LIGHT_GRAY);
        progressBar.setForeground(new Color(0,128,0));
        jFileButton=new JButton("上传视频");
        jFrame.setLayout(new BorderLayout());
        jFrame.add(jFileButton,BorderLayout.NORTH);
        jFrame.add(progressBar,BorderLayout.SOUTH);
        jFrame.add(scroll, BorderLayout.CENTER);
        //jFrame.add(jFileButton);
        //jFrame.add(progressBar);
        jFileButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                new BarThread(progressBar).start();
            }
            
        });
    }
    
    class BarThread extends Thread{
        JProgressBar progressBar;
        int localuploadstatus;
        public BarThread(JProgressBar progressBar){
            this.progressBar = progressBar;
        }
        @Override
		public void run(){

            // System.out.println("mouseClicked()"); // TODO
            // Auto-generated Event stub mouseClicked()
            JFileChooser jfChooser=new JFileChooser("F://..//..");
            jfChooser.setDialogTitle("打开并上传文件");
            /*jfChooser.setFileFilter(new FileFilter()
            {
                @Override
                public boolean accept(File f)
                {
                    if(f.isDirectory()) return true;
                    return false;
                }

                @Override
                public String getDescription()
                {
                    // TODO Auto-generated method stub
                    return "媒体文件(*.*)";
                }
            });*/
            int result=jfChooser.showOpenDialog(jFrame);
            if(result==JFileChooser.APPROVE_OPTION)
            { // 确认打开
                File fileIn=jfChooser.getSelectedFile();
                if(fileIn.exists())
                {
                    //JOptionPane.showMessageDialog(jFrame, "OPEN"); // 提示框
                    try
                    {
                        ResumeFTP myFtp=new ResumeFTP();
                        myFtp.setTextArea(textArea);
                        myFtp.setProgressBar(progressBar);
                        long l1=System.currentTimeMillis();
                        textArea.append("\n");
                        textArea.append("begin:"+l1);
                        textArea.paintImmediately(textArea.getBounds());
                        if(myFtp.connect("10.17.11.29",21,"hawkfly","zhengyong123"))
                        {
                            String remotePath="/";
                            String remoteFile=myFtp.getRemoteFileName(fileIn.getName());
                            if(remotePath==null||remotePath.trim().equals("")) remotePath="/";
                            //文件扩展名
                            String kzNm="";
                            if(remoteFile.indexOf(".")>=0) kzNm=remoteFile.substring(remoteFile.indexOf("."));
                            String cellCode="yp";
                            boolean isSaveFileName=false;
                            //若不保留原文件，则重新组装远程文件名
                            if(!isSaveFileName) remoteFile=cellCode+"_"+System.currentTimeMillis()+kzNm;
                            //获得远程路径最后一位
                            String lastStr=remotePath.substring(remotePath.length()-1);
                            if(lastStr.trim().equals("/"))
                                remoteFile=remotePath+cellCode+"/"+remoteFile;
                            else
                                remoteFile=remotePath+"/"+cellCode+"/"+remoteFile;
                            JSObject win = JSObject.getWindow(FileFtpApplet.this);
                            myFtp.upload(fileIn,remoteFile);
                            myFtp.disconnect();
                            win.eval("document.getElementById('videoUrl').value='"+remoteFile+"'");
                            win.eval("initScreenshot()");
                            long l2=System.currentTimeMillis();
                            textArea.append("\n");
                            textArea.append("end:"+l2);
                            textArea.append("\n");
                            textArea.append("remaining:"+(l2-l1));
                            textArea.paintImmediately(textArea.getBounds());
                        }
                    }catch(Exception e1)
                    {
                        e1.printStackTrace();
                        JOptionPane.showMessageDialog(jFrame,e1.getStackTrace());
                        textArea.append("\n");
                        textArea.append("连接FTP出错："+e1.getMessage());
                        textArea.paintImmediately(textArea.getBounds());
                    }
                }else
                {
                }
            }else if(result==JFileChooser.CANCEL_OPTION)
            {
                textArea.append("\n");
                textArea.append("Cancel button is pushed.");
                textArea.paintImmediately(textArea.getBounds());
            }else if(result==JFileChooser.ERROR_OPTION)
            {
                textArea.append("\n");
                textArea.append("Error when select file.");
                textArea.paintImmediately(textArea.getBounds());
            }
        }
    }
    
    @Override
	public void destroy() {
        // Put your code here
    }
    
    @Override
	public String getAppletInfo() {
        return "大文件FTP上傳Applet實現";
    }
    
    @Override
	public void init() {
        // Put your code here
    }
    
    @Override
	public void start() {
        // Put your code here
    }
    
    @Override
	public void stop() {
        // Put your code here
    }
}
